/*
 * ValueDocument.java
 *
 * Created on 6 de octubre de 2005, 10:25 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package ig.formato.util;

//import com.sun.java_cup.internal.version;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;

/**
 *
 * @author jsanchez
 */
public class ValueDocument extends PlainDocument {

    private String tipoValor = null;
    private Double mayorValor = null;
    private Double menorValor = null;
    DateVerifier dateVerifier = null;
    private Integer leng = null;

    public ValueDocument() {
        dateVerifier = new DateVerifier();
    }

    public ValueDocument(String tipo) {
        tipoValor = tipo.trim().toUpperCase();
    }

    public ValueDocument(String tipo, Integer leng) {
        tipoValor = tipo.trim().toUpperCase();
        this.leng = leng;
    }

    public ValueDocument(String tipo, Double mayor) {
        tipoValor = tipo.trim().toUpperCase();
        this.mayorValor = mayor;
    }

    public void setTipoValor(String tipo) {
        tipoValor = tipo.trim().toUpperCase();
    }

    public void setLeng(Integer leng) {
        this.leng = leng;
    }

    public void setMenorValor(Double menorValor) {
        if (menorValor != null) {
            this.menorValor = menorValor;
        } else {
            this.menorValor = null;
        }
    }

    public void setMayorValor(Double mayorValor) {
        if (mayorValor != null) {
            this.mayorValor = mayorValor;
        } else {
            this.mayorValor = null;
        }
    }
    private String CaracterAceptado = null;

    public void setCaracterAceptado(String CaracterAceptado) {
        this.CaracterAceptado = CaracterAceptado;
    }

    @Override
    public void insertString(int offset, String string, AttributeSet attributes) throws BadLocationException {
        if (string.trim().length() > 1) {
            int i=0;
            string=new StringBuilder(string).reverse().toString();
            while(i<string.trim().length()){
                insertString(offset, string.substring(i, i+1), attributes);
                i++;
            }
        } else {
            dateVerifier = new DateVerifier();
            if (string == null) {
                return;
            } else {
                boolean STRING = this.dateVerifier.verify(string);
                if (STRING) {
                    super.insertString(offset, string, attributes);
                    return;
                }
                String newValue;
                int length = getLength();
                if (length == 0) {
                    newValue = string;
                } else {
                    String currentContent = getText(0, length);
                    StringBuffer currentBuffer = new StringBuffer(currentContent);
                    currentBuffer.insert(offset, string);
                    newValue = currentBuffer.toString();
                }
                try {
                    if (this.leng == null || this.getLength() < this.leng) {
                        if (CaracterAceptado != null) {
                            if (string.equals(CaracterAceptado)) {
                                super.insertString(offset, string, attributes);
                                return;
                            } else if (tipoValor.equals("INTEGER")) {
                                Integer.parseInt(string);
                            } else if (tipoValor.equals("LONG")) {
                                Long.parseLong(string);
                            } else if (tipoValor.equals("DOUBLE")) {
                                Double.parseDouble(string);
                            }
                            super.insertString(offset, string, attributes);
                        } else {
                            if (tipoValor.equals("INTEGER")) {
                                Integer.parseInt(newValue);
                            } else if (tipoValor.equals("LONG")) {
                                Long.parseLong(newValue);
                            } else if (tipoValor.equals("DOUBLE")) {
                                Double.parseDouble(newValue);
                            }
                            if (mayorValor != null) {
                                if (Double.parseDouble(newValue) > mayorValor) {
                                    throw new NumberFormatException();
                                }
                            }
                            if (menorValor != null) {
                                if (Double.parseDouble(newValue) < menorValor) {
                                    throw new NumberFormatException();
                                }
                            }
                            super.insertString(offset, string, attributes);
                        }
                    } else {
                        throw new NumberFormatException();
                    }
                } catch (NumberFormatException exception) {
                    Toolkit.getDefaultToolkit().beep();
                }
            }
        }
    }
}